<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="demo1"></div>


    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    
    <script type="text/babel">
        //受控组件: 表单数据, 更改时存在状态中, 随用随取
        //推荐方式, 因为不用写 ref
        class Login extends React.Component {

            state = {
                username: '',
                password: ''
            }

            saveUsername = (event)=>{
                this.setState({username: event.target.value})
            }
            savePassword = (event)=>{
                this.setState({password: event.target.value})
            }

            render(){
                return (
                    <form action='https://www.baidu.com'>
                        用户名: <input onChange={this.saveUsername} type='text' name='username'/><br/>
                        密码: <input onChange={this.savePassword} type='password' name='password'/><br/>
                        <button onClick={this.doLogin}>登录</button>
                    </form>
                )
            }

            doLogin = (event)=>{
                event.preventDefault()
                const {username, password} = this.state
                alert(`username: ${username}, password: ${password}`)
            }
        }

        ReactDOM.render(<Login/>, document.getElementById('demo1'))
    </script>
</body>
</html>